package com.anhry.app.safety.manage.zczb.struts.action;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.zczb.bean.TbZczbBgd;
import com.anhry.app.safety.manage.zczb.bean.TbZczbCheck;
import com.anhry.app.safety.manage.zczb.bean.TbZczbCheckFj;
import com.anhry.app.safety.manage.zczb.bean.TbZczbHzd;
import com.anhry.app.safety.manage.zczb.bean.TbZczbHzdCheck;
import com.anhry.app.safety.manage.zczb.bean.TbZczbYh;
import com.anhry.app.safety.manage.zczb.struts.base.ZczbBaseAction;
import com.anhry.app.safety.util.Constants;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.UploadFile;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.UtilMethod;
import com.anhry.app.safety.util.bean.Actionlog;
import com.opensymphony.xwork2.ActionContext;

/**
 * @date:2012-10-24
 * @remark:规模以上企业自查情况信息（按照标准进行自查）
 * @author:dongbin
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class ZczbAction extends ZczbBaseAction{

	private Logger logger = Logger.getLogger(ZczbAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String hzdBackUser;//打回人
	private String hzdBackLy;//打回理由
	private TbZczbBgd bgd;
	private String cZg;
	private String itemXml;
	private String yhType;
	private TbZczbHzdCheck check;
	private TbZczbCheck checkInfo;//核查报告
	private String checkYear;
	private String checkQuarter;
	private String checkTitle;
	
	/**
	 * 规模以上企业已上报列表
	 * @return
	 */
	public String list(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		if(null!=this.getSearch() && "1".equals(this.getSearch())){num = 1;size = 10;}
		try {
			if("3".equals(this.cIsup)){//未上报的
				return this.unUpList();
			}
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String orderHql = "";//排序hql语句
			String hql = this.getQuaryHql(1);
			List objSize = this.utilService.getObjecList("TbZczbHzdView", hql,null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setObjList(this.utilService.getObjecList("TbZczbHzdView", hql,orderHql,page.getStartRow(),page.getSize()));
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList(this.enterprisService.getTypeList(user));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			this.setYearList(UtilMethod.getYearList(2012));
			if("1".equals(this.cIsup)){//上报的
				this.setResult("zczb_list");
			}else if("2".equals(this.cIsup)){//打回的
				this.setResult("zczb_back_list");
			}else{
				this.setResult("zczb_list");
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:list()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 未上报企业列表
	 * @return
	 */
	public String unUpList(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		if(null!=this.getSearch() && "1".equals(this.getSearch())){num = 1;size = 10;}
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String orderHql = " order by t.countyId,t.unitId,t.deptId,t.id";//排序hql语句
			String hql = this.getQuaryHql(2);
			List objSize = this.utilService.getObjecList("TbCorpInfoView1", hql,null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setObjList(this.utilService.getObjecList("TbCorpInfoView1", hql,orderHql,page.getStartRow(),page.getSize()));
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList(this.enterprisService.getTypeList(user));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("zczb_unup_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:unUpList()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 跳转到自查打回页面
	 * @return
	 */
	public String hzdBack(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					this.setResult("zczb_hzd_back");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBack()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 自查打回
	 * @return
	 */
	@Actionlog(logmsg="对企业上报信息进行打回")
	public String hzdBackSave(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					hzd.setHzdBackDate(new Date());
					hzd.setHzdBackLy(this.hzdBackLy);
					hzd.setHzdBackUser(this.hzdBackUser);
					hzd.setHzdStatus("2");
					this.utilService.update(hzd);
					this.utilService.deleteBySql("update tb_zczb_yh yh set yh.hzd_status=2 where yh.bgd_id in(select bgd.id from tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("update tb_zczb_yh_no yh set yh.hzd_status=2 where yh.bgd_id in(select bgd.id from tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.setTip("打回成功！");
					return this.hzdBack();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBackSave()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 删除企业上报记录
	 * @return
	 */
	public String hzdDele(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					this.utilService.deleteBySql("delete tb_zczb_yh_fj fj where fj.yh_id in(select yh.id from tb_zczb_yh yh where yh.bgd_id in(select bgd.id from tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+"))");
					this.utilService.deleteBySql("delete tb_zczb_yh yh where yh.bgd_id in(select bgd.id from tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("delete tb_zczb_yh_no yh where yh.bgd_id in(select bgd.id from tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("delete tb_zczb_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("delete tb_zczb_hzd_check t where t.hzd_id="+hzd.getId()+")");
					this.utilService.delete(hzd);
					this.setTip("删除成功！");
					return this.list();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdDele()异常："+e.getMessage());
		}
		this.setTip("你要删除的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 查看报告单列表
	 * @return
	 */
	public String bgdList(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbBgd t where 1=1 and t.hzdId="+this.hzdId+" order by t.bgdNum"));
				this.initLook();
				this.setResult("zczb_bgd_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:bgdList()异常："+e.getMessage());
		}
		this.setTip("您要查看的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 查看隐患列表
	 * @return
	 */
	public String yhFrame(){
		try {
			if(null!=this.bgdId && this.bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilService.getObjectById(TbZczbBgd.class, Long.parseLong(this.bgdId));
				if(null!=bgd){
					this.setBgd(bgd);
					this.setItemXml(this.zczbService.getItemXml(this.hzdId,this.bgdId,this.yhType,this.cZg));
					this.setResult("zczb_yh_frame");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:yhFrame()异常："+e.getMessage());
		}
		this.setTip("您要查看的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患列表
	 * @return
	 */
	public String yhList(){
		try {
			if(null!=this.bgdId && this.bgdId.length()>0){
				this.setObjList(this.zczbService.getYhList(hzdId, bgdId, cType, yhType, cZg));
				this.setResult("zczb_yh_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:yhList()异常："+e.getMessage());
		}
		this.setTip("您要查看的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患详细信息
	 * @return
	 */
	public String yhShow(){
		try {
			if(null!=this.yhId && this.yhId.length()>0){
				TbZczbYh yh = (TbZczbYh)this.utilService.getObjectById(TbZczbYh.class, Long.parseLong(this.yhId));
				if(null!=yh){
					this.setYh(yh);
					this.setFjList1(this.zczbService.getYhFjList("1",this.yhId));
					this.setFjList2(this.zczbService.getYhFjList("2",this.yhId));
					this.setFjList3(this.zczbService.getYhFjList("3",this.yhId));
					this.setResult("zczb_yh_show");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction yhShow()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 记录查看部门信息
	 *
	 */
	public void initLook(){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		TbZczbHzdCheck obj = null;
		try {
			if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					obj = this.zczbService.getHzdCheck(user,this.hzdId);
					if(null==obj){
						obj = new TbZczbHzdCheck();
						obj.setCheckStatus("0");
						obj.setHzdId(hzd.getId());
						obj.setIsDele("0");
						obj.setUserId(user.getId());
						obj.setLookDate(new Date());
						if(null!=unit){
							obj.setUnitId(unit.getId());
							obj.setUnitName(unit.getDeptName());
						}
						if(null!=dept){
							obj.setDeptId(dept.getId());
							obj.setDeptName(dept.getDeptName());
						}
						this.utilService.saveOrUpdate(obj);
						hzd.setHzdLooks(hzd.getHzdLooks()+1);
						this.utilService.update(hzd);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction initLook()异常："+e.getMessage());
		}
	}
	
	/**
	 * 检测该用户是否已经查看了该企业上报信息
	 *
	 */
	public void hzdLookCheck(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		try {
			if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
				if(null!=this.hzdId && !"".equals(this.hzdId)){
					TbZczbHzdCheck obj = this.zczbService.getHzdCheck(user, hzdId);
					if(null!=obj){
						this.contextPvd.getResponse().getWriter().write("1");
					}else{
						this.contextPvd.getResponse().getWriter().write("2");
					}
				}else{
					this.contextPvd.getResponse().getWriter().write("2");
				}
			}else{
				this.contextPvd.getResponse().getWriter().write("1");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction hzdLookCheck() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 跳转到自查核查页面
	 * @return
	 */
	public String hzdCheck(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
						TbZczbHzdCheck obj = this.zczbService.getHzdCheck(user, hzdId);
						if(null!=obj){
							this.setCheck(obj);
						}
					}
					this.setResult("zczb_hzd_check");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBack()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 核查记录保存
	 * @return
	 */
	public String hzdCheckSave(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbHzd hzd = (TbZczbHzd)this.utilService.getObjectById(TbZczbHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
						TbZczbHzdCheck obj = this.zczbService.getHzdCheck(user, hzdId);
						if(null!=obj){
							if("0".equals(obj.getCheckStatus())){//如果为查看状态
								hzd.setHzdChecks(hzd.getHzdChecks()+1);
								this.utilService.update(hzd);
							}
							if(null!=this.checkDate && this.checkDate.length()>0){
								obj.setCheckDate(DateFormater.formatDate(this.checkDate));
							}else{
								obj.setCheckDate(new Date());
							}
							obj.setCheckFlag1(checkFlag1);
							obj.setCheckFlag2(checkFlag2);
							obj.setCheckFlag3(checkFlag3);
							obj.setCheckFlag4(checkFlag4);
							obj.setCheckTel(checkTel);
							obj.setCheckMsg(checkMsg);
							obj.setCheckPer(checkPer);
							obj.setCheckStatus("1");
							this.utilService.update(obj);
						}
					}
					this.setTip("填写成功！");
					return this.hzdCheck();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBackSave()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 自查上报信息管理部门查看情况
	 * @return
	 */
	public String hzdLookShow(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbHzdCheck t where 1=1 and t.hzdId="+this.hzdId+" order by t.id desc"));
				this.setResult("zczb_look_show");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdLookShow()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 自查上报信息管理部门查看情况
	 * @return
	 */
	public String hzdCheckShow(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbHzdCheck t where 1=1 and t.checkStatus=1 and t.hzdId="+this.hzdId+" order by t.checkDate desc,t.id desc"));
				this.setResult("zczb_check_show");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdCheckShow()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 核查报告管理
	 * @return
	 */
	public String checkList(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		if(null!=this.getSearch() && "1".equals(this.getSearch())){num = 1;size = 10;}
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			TbUserDept unit = null;//用户单位
			TbUserDept dept = null;//用户部门
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if(null!=user.getTbUserDept()){
				dept = user.getTbUserDept();
			}
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int quarter = UtilMethod.getNowDate(new Date(), 1);
			int month = cal.get(Calendar.MONTH)+1;
			if(month<3){
				quarter = 4;
				year--;
			}else{
				quarter--;
			}
			if(null==this.cYear || "".equals(this.cYear)){
				this.setCYear(year+"");
			}
			if(null==this.cQuarter || "".equals(this.cQuarter)){
				this.setCQuarter(quarter+"");
			}
			StringBuilder hql = new StringBuilder(" where 1=1");
			//cType 1:属地、县(市、区)、开发区行业2、县(市、区)、开发区安委会、市行业3、市安委会
			if(null==this.cType || "".equals(this.cType)){//第一次登录为空
				if("2".equals(grade)){//如果当前用户是安办，且选中的为安办
					this.setCType("5");
				}else if("3".equals(grade) || "5".equals(grade)){//当前用户是市科室、市行业、县(市、区)、开发区安委会
					this.setCType("4");
				}else if("8".equals(grade)){//县(市、区)、开发区安委会
					this.setCType("3");
				}else if("4".equals(grade) || "6".equals(grade)){//当前用户是区科室、区行业
					this.setCType("2");
				}else if("7".equals(grade)){//属地
					this.setCType("1");
				}else{
					this.setCType("1");
				}
			}
			int tgrade = Integer.parseInt(grade);
			switch (tgrade) {
			case 0:hql.append(" and t.checkType="+this.cType);break;
			case 1:hql.append(" and t.checkType="+this.cType);break;
			case 2:hql.append(" and t.checkType="+this.cType);break;
			case 3: //市科室
				//如果是行业监管的科室
				if(Constants.DEPT_CLASS_OFFICE_INDUSTTR_SUPERVISION.equals(dept.getDeptClass()) && "2".equals(cType)) {
					//取得该行业部门主管的科室
					List<TbCorpType> corpTypes = (List)utilService.getHqlQuery("from TbCorpType t where t.unitId =" + dept.getId());
					StringBuilder quUnitIds = new StringBuilder();
					if(corpTypes != null) {
						for(TbCorpType ct: corpTypes) {
							if(StringUtils.isNotBlank(ct.getTypeQudeptIds()) ) {
								quUnitIds.append(ct.getTypeQudeptIds()).append(",");
							}
						}
					}
					
					hql.append("  and t.deptId in ("+quUnitIds.substring(0, quUnitIds.length()-1)+") and t.checkType="+this.cType);
				} else {
					hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId()+" and t.checkType="+this.cType);
				}
				break;
			case 4:
				if("4".equals(this.cType)){
					hql.append(" and t.unitId="+unit.getId()+" and t.checkType="+this.cType);
				}else{
					hql.append(" and t.sjId="+unit.getId()+" and t.checkType="+this.cType);
				}
				break;
			case 5:
				if("4".equals(this.cType)){
					hql.append(" and t.unitId="+unit.getId()+" and t.checkType="+this.cType);
				}else{
					hql.append(" and t.sjId="+unit.getId()+" and t.checkType="+this.cType);
				}
				break;
			case 6:hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId()+" and t.checkType="+this.cType);break;
			case 7:hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId()+" and t.checkType="+this.cType);break;
			case 8:
				if("3".equals(this.cType)){
					hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId()+" and t.checkType="+this.cType);break;
				}else{
					hql.append(" and t.checkType="+this.cType);
				}
				break;
			default:hql.append(" and t.checkType="+this.cType);break;
			}	
			hql.append(" and t.checkYear="+this.cYear+" and t.checkQuarter="+this.cQuarter+"");
			if(logger.isDebugEnabled()) {
				logger.debug("Checking report hql is -------> " + hql);
			}
			List objSize = this.utilService.getObjecList("TbZczbCheck", hql.toString(),null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setObjList(this.utilService.getObjecList("TbZczbCheck", hql.toString()," order by t.checkDate desc,t.id desc",page.getStartRow(),page.getSize()));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("check_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:checkList()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	//查看用户是否是区行业用户
	private void checkUserGrade() {
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		if(Constants.USER_GRADE_ID_OF_DISTRICT_INDUSTRY.equals(grade) || Constants.USER_GRADE_ID_OF_DISTRICT_OFFICE.equals(grade)) {
			//区 县行业不显示专题分析报告
			ActionContext.getContext().put("display", false);
		} else {
			ActionContext.getContext().put("display", true);
		}
	}
	/**
	 * 跳转到核查报告添加，修改页面
	 * @return
	 */
	public String checkEdit(){
		try {
			 checkUserGrade() ;
			
			if(null!=this.objId && this.objId.length()>0){
				TbZczbCheck check = (TbZczbCheck)this.utilService.getObjectById(TbZczbCheck.class, Long.parseLong(this.objId));
				if(null!=check){
					this.setCheckInfo(check);
					this.setFjList1((List)this.utilService.getHqlQuery("from TbZczbCheckFj t where 1=1 and t.fjType=1 and t.checkId="+this.objId));//核查报告
					this.setFjList2((List)this.utilService.getHqlQuery("from TbZczbCheckFj t where 1=1 and t.fjType=2 and t.checkId="+this.objId));//专题分析报告
					this.setCYear(check.getCheckYear());
					this.setCQuarter(check.getCheckQuarter());
				}
			}else{
				Calendar cal = Calendar.getInstance();
				int year = cal.get(Calendar.YEAR);
				int quarter = UtilMethod.getNowDate(new Date(), 1);
				int month = cal.get(Calendar.MONTH)+1;
				if(month<3){
					quarter = 4;
					year--;
				}else{
					quarter--;
				}
				this.setCYear(year+"");
				this.setCQuarter(quarter+"");
			}
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("check_edit");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:checkEdit()异常："+e.getMessage());
		}
		return ERROR;
	}
	/**
	 * 跳转到核查报告页面
	 * @return
	 */
	public String checkLook(){
		 checkUserGrade() ;
		try {
			if(null!=this.objId && this.objId.length()>0){
				TbZczbCheck check = (TbZczbCheck)this.utilService.getObjectById(TbZczbCheck.class, Long.parseLong(this.objId));
				if(null!=check){
					this.setCheckInfo(check);
					this.setFjList1((List)this.utilService.getHqlQuery("from TbZczbCheckFj t where 1=1 and t.fjType=1 and t.checkId="+this.objId));//核查报告
					this.setFjList2((List)this.utilService.getHqlQuery("from TbZczbCheckFj t where 1=1 and t.fjType=2 and t.checkId="+this.objId));//专题分析报告
					this.setCYear(check.getCheckYear());
					this.setCQuarter(check.getCheckQuarter());
					this.setResult("check_look");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:checkLook()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 删除核查报告
	 * @return
	 */
	public String checkDele(){
		try {
			if(null!=this.objId && this.objId.length()>0){
				TbZczbCheck check = (TbZczbCheck)this.utilService.getObjectById(TbZczbCheck.class, Long.parseLong(this.objId));
				if(null!=check){
					List list = (List)this.utilService.getHqlQuery("from TbZczbCheckFj t where 1=1 and t.fjType=1 and t.checkId="+this.objId);
					if(null!=list && list.size()>0){
						for(int i=0;i<list.size();i++){
							TbZczbCheckFj fj = (TbZczbCheckFj)list.get(i);
							UploadFile.removeFile(Util.SAFETY_FILE + File.separator + fj.getFjPath());
							this.utilService.delete(fj);
						}
					}
					this.utilService.delete(check);
					this.setTip("删除成功！");
					return checkList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:checkDele()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 核查报告保存
	 * @return
	 */
	public String checkSave(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			TbUserDept unit = null;//用户单位
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if("0".equals(grade) || "1".equals(grade)){//管理员不进行核查报告的保存
				this.setTip("保存成功！");
				return this.checkEdit();
			}
			if(null!=this.objId && this.objId.length()>0){
				TbZczbCheck obj = (TbZczbCheck)this.utilService.getObjectById(TbZczbCheck.class, Long.parseLong(this.objId));
				if(null!=obj){
					obj = this.initCheck(obj);
					this.utilService.update(obj);
					return this.saveCheckfj(obj, unit.getId().toString());
				}
			}else{
				TbZczbCheck obj = new TbZczbCheck();
				obj = this.initCheck(obj);
				obj.setCheckDate(new Date());
				obj.setIsDele("0");
				this.utilService.save(obj);
				return this.saveCheckfj(obj, unit.getId().toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:checkEdit()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 保存信息
	 * @param obj
	 * @return
	 */
	public TbZczbCheck initCheck(TbZczbCheck obj){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		try {
			if("2".equals(grade)){//如果当前用户是安办，且选中的为安办
				obj.setCheckType("5");
			}else if("3".equals(grade) || "5".equals(grade)){//当前用户是市科室、市行业
				obj.setCheckType("4");
			}else if("8".equals(grade)){//县(市、区)、开发区安委会
				obj.setCheckType("3");
			}else if("4".equals(grade) || "6".equals(grade)){//当前用户是区科室、区行业
				if(null!=dept){
					obj.setSjId(dept.getDeptParentId());
				}
				obj.setCheckType("2");
			}else if("7".equals(grade)){//属地
				obj.setCheckType("1");
			}
			obj.setCheckQuarter(this.checkQuarter);
			obj.setCheckMsg(this.checkMsg);
			obj.setCheckTitle(this.checkTitle);
			obj.setCheckPer(this.checkPer);
			obj.setCheckTel(this.checkTel);
			obj.setCheckYear(this.checkYear);
			if(null!=unit){
				obj.setUnitId(unit.getId());
				obj.setUnitName(unit.getDeptName());
			}
			if(null!=dept){
				obj.setDeptId(dept.getId());
				obj.setDeptName(dept.getDeptName());
			}
			obj.setUserId(user.getId());
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: TbZczbCheck initCheck(TbZczbCheck obj)异常："+e.getMessage());
		}
		return obj;
	}
	
	/**
	 * 隐患附件保存
	 * @param yh
	 */
	public String saveCheckfj(TbZczbCheck check,String unitId){
		try {
			if(null!=this.fjFile && this.fjFile.length>0){
				for(int i=0;i<this.fjFile.length;i++){
					TbZczbCheckFj fj = new TbZczbCheckFj();
					fj.setFjTitle(this.fjFileFileName[i]);
					String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB_CHECK;
					String fileName = UploadFile.copy(this.fjFile[i], tem_path, this.fjFileFileName[i],unitId);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB_CHECK+File.separator+unitId+File.separator+fileName;
					fj.setFjPath(path);
					fj.setIsDele("0");
					fj.setFjType("1");
					fj.setCheckId(check.getId());
					this.utilService.save(fj);
				}
			}
			if(null!=this.docFile && this.docFile.length>0){
				for(int i=0;i<this.docFile.length;i++){
					TbZczbCheckFj fj = new TbZczbCheckFj();
					fj.setFjTitle(this.docFileFileName[i]);
					String tem_path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.ZCZB_CHECK;
					String fileName = UploadFile.copy(this.docFile[i], tem_path, this.docFileFileName[i],unitId);
					String path = Util.FILE_FOLDER+File.separator+Util.ZCZB_CHECK+File.separator+unitId+File.separator+fileName;
					fj.setFjPath(path);
					fj.setIsDele("0");
					fj.setFjType("2");
					fj.setCheckId(check.getId());
					this.utilService.save(fj);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction saveYhfj(TbZczbYh yh) 异常："+e.getMessage());
		}
		this.setTip("操作成功！");
		return this.checkEdit();
	}
	
	/**
	 * 异步删除附件
	 *
	 */
	public void deleCheckFj(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.getFjId() && !"".equals(this.getFjId())){
				TbZczbCheckFj fj = (TbZczbCheckFj)this.utilService.getObjectById(TbZczbCheckFj.class, Long.parseLong(this.fjId));
				if(null!=fj){
					UploadFile.removeFile(Util.SAFETY_FILE + File.separator + fj.getFjPath());
					this.utilService.delete(fj);
				}
				this.contextPvd.getResponse().getWriter().write("1");
			}else{
				this.contextPvd.getResponse().getWriter().write("2");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction deleCheckFj() 异常："+e.getMessage());
		}
	}
	
	public String getQuaryHql(int type){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		StringBuilder urls = new StringBuilder("");
		StringBuilder hql = new StringBuilder(" where 1=1 ");
		String key = "";
		if(null!=this.keyWord && this.keyWord.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				key = this.getKeyWord().trim();
			}else{
				key = this.contextPvd.decode(this.getKeyWord());	
			}
			this.setKeyWord(key);
			hql.append(" and t.corpName like '%"+key+"%'");
			urls.append("keyWord="+this.contextPvd.encode(this.keyWord));
		}else{
			urls.append("keyWord=");
		}
		
		if("3".equals(grade) && "3".equals(dept.getDeptClass())){//市科室行业部门
			hql.append(" and t.deptMainId="+dept.getId());
			urls.append("&cHy="+dept.getId().toString());
			this.setCHy(dept.getId().toString());
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("4".equals(grade) && "3".equals(dept.getDeptClass())){//县区科室行业部门
			if((null==this.cMaxType || "".equals(this.cMaxType)) && (null==this.cMinType || "".equals(this.cMinType))){
				hql.append(" and t.quDeptIds like '%"+dept.getId()+"%'");
			}else{
				if(null!=this.cMaxType && this.cMaxType.length()>0){
					hql.append(" and t.maxTypeId="+this.cMaxType);
					urls.append("&cMaxType="+this.cMaxType);
				}
				if(null!=this.cMinType && this.cMinType.length()>0){
					hql.append(" and t.minTypeId="+this.cMinType);
					urls.append("&cMinType="+this.cMinType);
				}
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}else if("5".equals(grade)){//市行业
			if("1".equals(unit.getDeptClass())){//行业监管
				hql.append(" and t.deptMainId="+unit.getId());
				urls.append("&cHy="+unit.getId().toString());
				this.setCHy(unit.getId().toString());
			}else{//专业监管
				if("5310".equals(unit.getId().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(unit.getId().toString())){//消防局
					
				}else if("5312".equals(unit.getId().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("6".equals(grade)){//区行业
			if("1".equals(dept.getDeptClass())){
				hql.append(" and t.deptMainId="+dept.getDeptSj());
				urls.append("&cHy="+dept.getDeptSj().toString());
				this.setCHy(dept.getDeptSj().toString());
			}else{
				if("5310".equals(dept.getDeptSj().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(dept.getDeptSj().toString())){//消防局
					
				}else if("5312".equals(dept.getDeptSj().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else{//其他用户
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}
		
		if("4".equals(grade) || "6".equals(grade) || "8".equals(grade)){//县区安委会、区科室、区行业
			hql.append(" and t.corpLixu not like '40'");
			hql.append(" and t.countyId="+unit.getId());
			this.setCCounty(unit.getId().toString());
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else if("7".equals(grade)){//属地
			hql.append(" and t.countyId="+unit.getId()+" and t.unitId="+dept.getId());
			this.setCCounty(unit.getId().toString());
			this.setCUnit(dept.getId().toString());
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else{
			if(null!=this.cCounty && this.cCounty.length()>0){
				hql.append(" and t.countyId="+this.cCounty);
				urls.append("&cCounty="+this.cCounty);
			}
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}
		if(null!=this.cZcType && this.cZcType.length()>0){
			hql.append(" and t.corpReportType="+this.cZcType);
			urls.append("&cZcType="+this.cZcType);
		}
		if(null!=this.cGrade && this.cGrade.length()>0){
			if("E".equals(this.cGrade)){
				hql.append(" and t.corpGrade is null");
			}else{
				hql.append(" and t.corpGrade='"+this.cGrade+"'");
			}
			urls.append("&cGrade="+this.cGrade);
		}
		if(null==this.cYear || "".equals(this.cYear)){
			this.setCYear(UtilMethod.getZcYear());
		}
		if(null==this.cMonth || "".equals(this.cMonth)){
			this.setCMonth(UtilMethod.getZclMonth());
		}
		if(null==this.cReportType || "".equals(this.cReportType)){
			this.setCReportType("3");
		}
		if(type==1){
			hql.append(" and t.hzdYear="+this.cYear);
			urls.append("&cYear="+this.cYear);
			hql.append(" and t.hzdMonth="+this.cMonth);
			urls.append("&cMonth="+this.cMonth);
			if(null!=this.cReportType && this.cReportType.length()>0){
				if("0".equals(this.cReportType)){
					if(null!=this.cHalf && this.cHalf.length()>0){
						hql.append(" and t.reportType=0 and t.hzdHalf="+this.cHalf);	
					}
				}else if("1".equals(this.cReportType)){
					hql.append(" and t.reportType=1");
				}
				urls.append("&cReportType="+this.cReportType);
			}
			if(null!=this.cHecha && this.cHecha.length()>0){
				if("1".equals(this.cHecha)){
					hql.append(" and t.hzdLooks=0");
				}else if("2".equals(this.cHecha)){
					hql.append(" and t.hzdChecks>0");
				}else if("3".equals(this.cHecha)){
					hql.append(" and t.hzdLooks=0");
				}else if("4".equals(this.cHecha)){
					hql.append(" and t.hzdChecks>0");
				}
				urls.append("&cHecha="+this.cHecha);
			}
			if(null!=this.cYh && this.cYh.length()>0){
				if("1".equals(this.cYh)){
					hql.append(" and t.hzdYhNum=0");
				}else if("2".equals(this.cYh)){
					hql.append(" and t.hzdSameNum>0");
				}else if("3".equals(this.cYh)){
					hql.append(" and t.hzdBigNum>0");
				}else if("4".equals(this.cYh)){
					hql.append(" and t.hzdWclNum>0");
				}
				urls.append("&cYh="+this.cYh);
			}
			if(null==this.cIsup || "".equals(this.cIsup)){
				this.setCIsup("1");
			}
			hql.append(" and t.hzdStatus="+this.cIsup);
			//urls.append("&cIsup="+this.cIsup);
		}else{
			if(null!=this.cReportType && this.cReportType.length()>0){
				if("0".equals(this.cReportType)){
					if(null!=this.cHalf && this.cHalf.length()>0){
			           hql.append(" and t.reportType=0 and not exists(from TbZczbHzd x where x.corpId=t.id and x.hzdYear="+this.cYear+" and x.hzdMonth="+this.cMonth+" and x.hzdHalf="+this.cHalf+" and x.hzdStatus=1)");
			           urls.append("&cHalf="+this.cHalf);
			     	}
				}else if("1".equals(this.cReportType)){
					  hql.append("  and t.reportType=1 and not exists(from TbZczbHzd x where x.corpId=t.id and x.hzdYear="+this.cYear+" and x.hzdMonth="+this.cMonth+" and x.hzdStatus=1)");
					  urls.append("&cYear="+this.cYear);
				}else{
					  hql.append(" and not exists(from TbZczbHzd x where x.corpId=t.id and x.hzdYear="+this.cYear+" and x.hzdMonth="+this.cMonth+" and x.hzdStatus=1)");
				}
			}
			urls.append("&cYear="+this.cYear);
			urls.append("&cMonth="+this.cMonth);
			urls.append("&cReportType="+this.cReportType);
			hql.append(" and t.corpReportType=1");
			hql.append(" and ((t.corpAddTime <= to_date('").append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.isDele=0) ")
			.append(" or (t.corpAddTime <= to_date('").append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.updateTime >= to_date('")
			.append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.isDele=1)) ");
		}
		urls.append("&cIsup="+cIsup);
		this.setUrls(urls.toString());
		
		if(logger.isDebugEnabled()) {
			logger.debug("Query hidden danger hql is ----- " + hql.toString());
		}
		return hql.toString();
	}
	

	public String getHzdBackLy() {
		return hzdBackLy;
	}

	public void setHzdBackLy(String hzdBackLy) {
		this.hzdBackLy = hzdBackLy;
	}

	public String getHzdBackUser() {
		return hzdBackUser;
	}

	public void setHzdBackUser(String hzdBackUser) {
		this.hzdBackUser = hzdBackUser;
	}

	public TbZczbBgd getBgd() {
		return bgd;
	}

	public void setBgd(TbZczbBgd bgd) {
		this.bgd = bgd;
	}

	public String getCZg() {
		return cZg;
	}

	public void setCZg(String zg) {
		cZg = zg;
	}

	public String getItemXml() {
		return itemXml;
	}

	public void setItemXml(String itemXml) {
		this.itemXml = itemXml;
	}

	public String getYhType() {
		return yhType;
	}

	public void setYhType(String yhType) {
		this.yhType = yhType;
	}

	public TbZczbHzdCheck getCheck() {
		return check;
	}

	public void setCheck(TbZczbHzdCheck check) {
		this.check = check;
	}

	public TbZczbCheck getCheckInfo() {
		return checkInfo;
	}

	public void setCheckInfo(TbZczbCheck checkInfo) {
		this.checkInfo = checkInfo;
	}

	public String getCheckQuarter() {
		return checkQuarter;
	}

	public void setCheckQuarter(String checkQuarter) {
		this.checkQuarter = checkQuarter;
	}

	public String getCheckTitle() {
		return checkTitle;
	}

	public void setCheckTitle(String checkTitle) {
		this.checkTitle = checkTitle;
	}

	public String getCheckYear() {
		return checkYear;
	}

	public void setCheckYear(String checkYear) {
		this.checkYear = checkYear;
	}

}
